/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.bff.designtime.generator.entity;

import com.inspur.edp.bef.bizentity.GspBizEntityObject;
import com.inspur.edp.bff.designtime.generator.VmGeneratorContext;
import com.inspur.edp.bff.designtime.generator.changeconvertor.FSChangeConvertorGenerator;
import com.inspur.edp.bff.designtime.generator.defaultvalue.converter.DefaultValueConvertorGen;
import com.inspur.edp.bff.designtime.generator.defaultvalue.mapping.DefaultValueMappingGen;
import com.inspur.edp.bff.designtime.generator.entity.defaultvalue.DefaultValueGenerator;
import com.inspur.edp.bff.designtime.generator.entity.help.HelpMappingGenerator;
import com.inspur.edp.bff.designtime.generator.entity.node.VmExtendNodeGenerator;
import com.inspur.edp.bff.designtime.generator.entitydataconvertor.VMObjectDataConvertorGenerator;
import com.inspur.edp.bff.designtime.generator.filter.FilterConvertorGenerator;
import com.inspur.edp.caf.generator.BaseGenerator;
import com.inspur.edp.cef.generator.entityGenerator.entity.CMEntityGeneratorContext;
import com.inspur.edp.cef.generator.entityGenerator.entity.CommonModelEntityGenerator;
import com.inspur.edp.cef.generator.entityGenerator.entity.EntityNodeGenerator;
import com.inspur.edp.cef.generator.overall.commonModel.CommonModelGeneratorContext;
import com.inspur.edp.formserver.viewmodel.GspViewObject;

import java.util.ArrayList;

import static com.inspur.edp.cef.designtime.api.variable.VariableSourceType.Context;

public class VmEntityGenerator extends CommonModelEntityGenerator {

  private VmGeneratorContext vmGenContext;

  public VmEntityGenerator(VmGeneratorContext vmGenContext) {
    super(vmGenContext, vmGenContext.getVm(), vmGenContext.getParentVo());
    this.vmGenContext = vmGenContext;
  }

  //C# TO JAVA CONVERTER WARNING: There is no Java equivalent to C#'s shadowing via the 'new' keyword:
//ORIGINAL LINE: internal new VmEntityGenContext getContext() { return (VmEntityGenContext)super.context; }
  public VmEntityGenContext getContext() {
    return (VmEntityGenContext) super.getContext();
  }

  @Override
  protected String getPackageName() {
    return vmGenContext.getVm().getEntityAssemblyInfo().getDefaultNamespace();
  }

//  @Override
//  protected java.util.ArrayList<Assembly> addReferences() {
//    if (vmGenContext.getVm().IsVirtual) {
//      return super.addReferences();
//    }
//    java.util.ArrayList<Assembly> list = super.addReferences();
//    if (list == null) {
//      list = new java.util.ArrayList<Assembly>();
//    }
//    if (vmGenContext.getRefEntityAssembly() != null) {
//      list.add(vmGenContext.getRefEntityAssembly());
//    }
//    return list;
//  }

  @Override
  protected EntityNodeGenerator createEntityNodeGenerator() {
    return new VmExtendNodeGenerator(vmGenContext.getVm().getMainObject(), getContext());
  }
  //protected override NodeGeneratorBase getNodeGenerator()
  //{
  //    return new VmExtendNodeGenerator(ParentCtx.Model.MainObject, Context, vmGenContext);
  //}

  @Override
  protected java.util.ArrayList<BaseGenerator> createChildGenerators() {
//Java版临时覆盖：QO。
   ArrayList<BaseGenerator> list = super.createChildGenerators();
//
//    if (vmGenContext.getrefQ() != null) {
//      addQoGenerators(list);
//    } else {
      addBeGenerators(list);
//    }
//
    return list;
  }

//  private void addQoGenerators(java.util.ArrayList<BaseGenerator> generators) {
//    generators.add(new VMObjectDataConvertorGenerator(vmGenContext.getVm().MainObject,
//        vmGenContext.RefQueryNode, vmGenContext, Context, true));
//  }

  private void addBeGenerators(java.util.ArrayList<BaseGenerator> generators) {
    if (!vmGenContext.getVm().getMainObject().getIsVirtual()
        && vmGenContext.getVm().getMainObject().getMapping().getTargetMetadataId() .equals( vmGenContext
        .getRefBizEntity().getID())) {
      GspBizEntityObject refBeObject = getRefBeObject(vmGenContext.getVm().getMainObject());
     generators.add(
         new VMObjectDataConvertorGenerator(vmGenContext.getVm().getMainObject(), refBeObject,
             vmGenContext, getContext(), true));
     generators.add(
         new FSChangeConvertorGenerator(vmGenContext.getVm().getMainObject(), refBeObject, vmGenContext,
                 getContext(), true));
     generators.add(
         new DefaultValueMappingGen(vmGenContext.getVm().getMainObject(), refBeObject, vmGenContext,
             getContext()));
     if(vmGenContext.getVm().getIsGenFilterConvertor()) {
       generators.add(
           new FilterConvertorGenerator(vmGenContext.getVm().getMainObject(), refBeObject, vmGenContext,
               getContext(), true));
     }
    } else {
      if(vmGenContext.getVm().getIsGenFilterConvertor()) {
        generators.add(
            new FilterConvertorGenerator(vmGenContext.getVm().getMainObject(), null, vmGenContext, getContext(),
                true));
      }
    }

    generators.add(new HelpMappingGenerator(getContext(), vmGenContext));
    //generators.Add(new HelpActionRecordGenerator(Context, vmGenContext));
    //Java版临时替换：默认值
    generators.add(new DefaultValueGenerator(getContext(), vmGenContext));
    generators.add(new DefaultValueConvertorGen(vmGenContext, getContext()));
  }

    private GspBizEntityObject getRefBeObject(GspViewObject vmObject) {
//C# TO JAVA CONVERTER TODO TASK: There is no equivalent to implicit typing in Java:
      GspBizEntityObject refBeObject = vmGenContext.getRefBizEntity().getMainObject();
        Object tempVar = vmGenContext.getRefBizEntity().findObjectById(vmObject.getMapping().getTargetObjId());
        refBeObject = (GspBizEntityObject) ((tempVar instanceof GspBizEntityObject) ? tempVar : null);
        //vmGenContext.RefBizEntity.MainObject.GetChildObjectById(vmObject.Mapping.TargetObjId) as GspBizEntityObject;

      return refBeObject;
  }

  @Override
  protected CMEntityGeneratorContext createEntityGeneratorContext() {
    VmEntityGenContext context = new VmEntityGenContext(vmGenContext);
    context.setRefBizEntity(vmGenContext.getRefBizEntity());
    //context.BinPath = vmGenContext.BinPath;
    //context.GeneratePath = vmGenContext.VoGeneratePath;
    vmGenContext.setEntityContext(context);
    vmGenContext.setEntityGenContext (context);
    return context;
  }

  //protected override void afterGenerate()
  //{
  //    base.AfterGenerate();
  //    //vmGenContext.NodeTypes = ParentCtx.NodeTypes;
  //    //vmGenContext.NodeConsts = ParentCtx.NodeConsts;
  //}

  @Override
  protected void beforeGenerate() {
    super.beforeGenerate();
  }
}
